<template>
    <div id="app">
      <el-form :model="food" label-width="100px">
        <el-form-item label="编号" prop="id">
          <el-input v-model="food.id"></el-input>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="food.name"></el-input>
        </el-form-item>
        <el-form-item label="类别">
          <el-select v-model="food.cid" placeholder="请选择">
          <el-option
            v-for="item in categoryList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="原价" prop="oldPrice">
          <el-input v-model="food.oldPrice"></el-input>
        </el-form-item>

        <el-form-item label="优惠价格" prop="tel">
          <el-input v-model="food.price"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <el-upload
            class="upload-demo"
            action="http://localhost:8989/food/uploadAttach"
            :limit="1"
            :on-exceed="handleOnExceed"
            name="attach"
            accept=".png,.jpg"
            :multiple="false"
            :on-success="handleOnSuccess"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>

        <el-form-item label="描述" prop="tel">
          <el-input v-model="food.descrption" type="textarea" rows="5"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click.prevent="test">保存</el-button>
        </el-form-item>
      </el-form>
    </div>
</template>

<script>
    export default {
      data(){
        return {
          food:{},
          categoryList:[]
        }
      },
      created() {
        this.axios.get("/category/getAll").then((resp)=>{
          this.categoryList=resp.data;
        });
      },
      methods:{
        test(){

            this.$confirm("是否确认提交","保存提示").then(()=>{
              this.axios.post("/food/add",this.food).then((resp)=>{
                  this.$message.success("保存成功！");
                  this.$router.push("/food/FoodList")
                });
            });

        },
        handleOnExceed(){
          console.log("文件个数超出限制");
        },
        handleOnSuccess(resp){
          //文件上传成功之后要把文件上传到哪传给brand对象
          this.food.pic=resp;
        },
      }
    }
</script>


<style scoped>

</style>
